﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;

namespace Xceed.Silverlight.Data
{
  internal sealed class ReadOnlyGroupCollection : IGroupCollection
  {
    #region Constructor

    internal ReadOnlyGroupCollection( IGroupCollection collection )
    {
      if( collection == null )
        throw new ArgumentNullException( "collection" );

      m_collection = collection;
      m_collection.CollectionChanging += new EventHandler<GroupCollectionChangedEventArgs>( this.OnCollectionChanging );
      m_collection.CollectionChanged += new EventHandler<GroupCollectionChangedEventArgs>( this.OnCollectionChanged );
    }

    #endregion

    #region GenericCollection Private Property

    private ICollection<DataPath> GenericCollection
    {
      get
      {
        return m_collection;
      }
    }

    #endregion

    #region Collection Private Property

    private ICollection Collection
    {
      get
      {
        return m_collection;
      }
    }

    #endregion

    #region GenericEnumerable Private Property

    private IEnumerable<DataPath> GenericEnumerable
    {
      get
      {
        return m_collection;
      }
    }

    #endregion

    #region Enumerable Private Property

    private IEnumerable Enumerable
    {
      get
      {
        return m_collection;
      }
    }

    #endregion

    public override int GetHashCode()
    {
      return m_collection.GetHashCode();
    }

    public override bool Equals( object obj )
    {
      if( object.ReferenceEquals( this, obj ) )
        return true;

      ReadOnlyGroupCollection groupCollection = obj as ReadOnlyGroupCollection;
      if( groupCollection == null )
        return false;

      return object.Equals( m_collection, groupCollection.m_collection );
    }

    private void OnCollectionChanging( object sender, GroupCollectionChangedEventArgs e )
    {
      IGroupCollection collection = sender as IGroupCollection;
      if( collection == null )
        return;

      EventHandler<GroupCollectionChangedEventArgs> handlers = this.CollectionChanging;

      if( handlers != null )
      {
        handlers.Invoke( this, e );
      }
    }

    private void OnCollectionChanged( object sender, GroupCollectionChangedEventArgs e )
    {
      IGroupCollection collection = sender as IGroupCollection;
      if( collection == null )
        return;

      EventHandler<GroupCollectionChangedEventArgs> handlers = this.CollectionChanged;

      if( handlers != null )
      {
        handlers.Invoke( this, e );
      }
    }

    #region IGroupCollection Members

    bool IGroupCollection.IsCollapsedByDefault
    {
      get
      {
        return m_collection.IsCollapsedByDefault;
      }
    }

    Nullable<bool> IGroupCollection.AnyCollapsedGroup
    {
      get
      {
        return m_collection.AnyCollapsedGroup;
      }
    }

    Nullable<bool> IGroupCollection.AnyExpandedGroup
    {
      get
      {
        return m_collection.AnyExpandedGroup;
      }
    }

    public event EventHandler<GroupCollectionChangedEventArgs> CollectionChanging;
    public event EventHandler<GroupCollectionChangedEventArgs> CollectionChanged;

    void IGroupCollection.Collapse( DataPath item )
    {
      throw new NotSupportedException();
    }

    void IGroupCollection.CollapseAll()
    {
      throw new NotSupportedException();
    }

    void IGroupCollection.Expand( DataPath item )
    {
      throw new NotSupportedException();
    }

    void IGroupCollection.ExpandAll()
    {
      throw new NotSupportedException();
    }

    #endregion

    #region ICollection<DataPath> Members

    int ICollection<DataPath>.Count
    {
      get
      {
        return this.GenericCollection.Count;
      }
    }

    bool ICollection<DataPath>.IsReadOnly
    {
      get
      {
        return true;
      }
    }

    void ICollection<DataPath>.Clear()
    {
      throw new NotSupportedException();
    }

    bool ICollection<DataPath>.Contains( DataPath item )
    {
      return m_collection.Contains( item );
    }

    void ICollection<DataPath>.Add( DataPath item )
    {
      throw new NotSupportedException();
    }

    bool ICollection<DataPath>.Remove( DataPath item )
    {
      throw new NotSupportedException();
    }

    void ICollection<DataPath>.CopyTo( DataPath[] array, int arrayIndex )
    {
      this.GenericCollection.CopyTo( array, arrayIndex );
    }

    #endregion

    #region ICollection Members

    object ICollection.SyncRoot
    {
      get
      {
        return this.Collection.SyncRoot;
      }
    }

    bool ICollection.IsSynchronized
    {
      get
      {
        return this.Collection.IsSynchronized;
      }
    }

    int ICollection.Count
    {
      get
      {
        return this.Collection.Count;
      }
    }

    void ICollection.CopyTo( Array array, int index )
    {
      this.Collection.CopyTo( array, index );
    }

    #endregion

    #region IEnumerable<DataPath> Members

    IEnumerator<DataPath> IEnumerable<DataPath>.GetEnumerator()
    {
      return this.GenericEnumerable.GetEnumerator();
    }

    #endregion

    #region IEnumerable Members

    IEnumerator IEnumerable.GetEnumerator()
    {
      return this.Enumerable.GetEnumerator();
    }

    #endregion

    #region Private Fields

    private readonly IGroupCollection m_collection;

    #endregion
  }
}
